package com.yanxinim.cn.store;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.yanxinim.cn.R;
import com.yanxinim.cn.fragment.OrderListFragment;
import com.yanxinim.cn.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StoreOrderListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.rbtn_1)
    RadioButton rbtn1;
    @BindView(R.id.rbtn_2)
    RadioButton rbtn2;
    @BindView(R.id.rbtn_3)
    RadioButton rbtn3;
    @BindView(R.id.rbtn_4)
    RadioButton rbtn4;
    @BindView(R.id.view_pager_order)
    ViewPager viewPagerOrder;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.rbtn_0)
    RadioButton rbtn0;
    private List<Fragment> fragmentList = new ArrayList<>();
    private OrderListFragment orderListFragment, orderListFragment0, orderListFragment1, orderListFragment2, orderListFragment3;
    private OrderListFragment nowFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_store_order_list_activity);
        ButterKnife.bind(this);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        radioGroup.check(R.id.rbtn_0);
        nowFragment = orderListFragment;
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rbtn_0:
                        viewPagerOrder.setCurrentItem(0);
                        break;
                    case R.id.rbtn_1:
                        viewPagerOrder.setCurrentItem(1);
                        break;
                    case R.id.rbtn_2:
                        viewPagerOrder.setCurrentItem(2);
                        break;
                    case R.id.rbtn_3:
                        viewPagerOrder.setCurrentItem(3);
                        break;
                    case R.id.rbtn_4:
                        viewPagerOrder.setCurrentItem(4);
                        break;
                }
            }
        });
        String userId = getUser().getUserId();
        orderListFragment = OrderListFragment.getInstance("-1",userId);
        orderListFragment0 = OrderListFragment.getInstance("0",userId);
        orderListFragment1 = OrderListFragment.getInstance("1",userId);
        orderListFragment2 = OrderListFragment.getInstance("2",userId);
        orderListFragment3 = OrderListFragment.getInstance("3",userId);
        fragmentList.add(orderListFragment);
        fragmentList.add(orderListFragment0);
        fragmentList.add(orderListFragment1);
        fragmentList.add(orderListFragment2);
        fragmentList.add(orderListFragment3);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPagerOrder.setAdapter(viewPagerAdapter);

        viewPagerOrder.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPagerOrder.setCurrentItem(position);
                switch (position){
                    case 0:
                        nowFragment = orderListFragment;
                        radioGroup.check(R.id.rbtn_0);
                        break;
                    case 1:
                        nowFragment = orderListFragment0;
                        radioGroup.check(R.id.rbtn_1);
                        break;
                    case 2:
                        nowFragment = orderListFragment1;
                        radioGroup.check(R.id.rbtn_2);
                        break;
                    case 3:
                        nowFragment = orderListFragment2;
                        radioGroup.check(R.id.rbtn_3);
                        break;
                    case 4:
                        nowFragment = orderListFragment3;
                        radioGroup.check(R.id.rbtn_4);
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (nowFragment != null){
            nowFragment.refreshData();
        }
    }
}
